package com.dong.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dong.handler.BusinessException;
import com.dong.response.ResponseResult;
import com.dong.response.ResultCode;
import com.dong.response.ResultCodeEnum;
import com.dong.system.entity.User;
import com.dong.system.service.UserService;
import com.dong.system.vo.AddUserVO;
import com.dong.system.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author dongjian
 * @since 2021-01-19
 */
@RestController
@RequestMapping("/system/tb-user")
@Api(value = "系统用户模块", tags = "系统用户接口")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/findUsers")
    @ApiOperation(value = "用户列表", notes = "查询所有用户信息")
    public ResponseResult findAllUsers() {
        // 制造算术异常
        // int i = 1 / 0;
        List<User> userList = userService.list();
        // 筛选掉逻辑删除的数据
        userList = userList.stream().filter(u -> u.getDeleted() != 1).collect(Collectors.toList());
        return ResponseResult.ok().data("users", userList);
    }

    @GetMapping("/getUser/{id}")
    @ApiOperation(value = "查询单个用户", notes = "通过ID查询对应的用户信息")
    public ResponseResult getUserById(@PathVariable("id") Long id) {
        User user = userService.getById(id);
        if (Objects.nonNull(user) && user.getDeleted() != 1) {
            return ResponseResult.ok().data("user", user);
        } else {
            // 抛出业务异常
            throw new BusinessException(ResultCodeEnum.USER_NOT_FOUND_EXCEPTION.getCode(),
                    ResultCodeEnum.USER_NOT_FOUND_EXCEPTION.getMessage());
        }
    }

    @PostMapping("/findUserList")
    @ApiOperation(value = "分页-用户列表", notes = "分页查询用户列表")
    public ResponseResult findUserList(@RequestParam(required = true, defaultValue = "1") Integer currentPage,
                                       @RequestParam(required = true, defaultValue = "7") Integer size,
                                       @RequestBody UserVO userVO) {

        // 前端查询条件封装
        IPage<User> userPage = userService.findUserPage(userVO, currentPage, size);
        long total = userPage.getTotal();
        List<User> records = userPage.getRecords();
        return ResponseResult.ok().data("total", total).data("users", records);
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "添加用戶", notes = "将输入的用户信息添加到用户表中")
        public ResponseResult addUser(@RequestBody @Validated AddUserVO addUserVO){
        userService.addUser(addUserVO);
        return ResponseResult.ok();
    }

    @ApiOperation(value = "用户状态", notes = "只有启用/禁用两种状态")
    @PutMapping("/updateStatus/{id}/{status}")
    public ResponseResult updateUserStatus(@PathVariable Long id, @PathVariable int status) {
        userService.updateStatus(id, status);
        return ResponseResult.ok();
    }

    @ApiOperation(value = "用户操作", notes = "删除用户")
    @DeleteMapping("/delete/{id}")
    public ResponseResult deleteUser(@PathVariable Long id) {
        userService.deleteUserById(id);
        return ResponseResult.ok();
    }

    @ApiOperation(value = "更新用户", notes = "更新用户信息")
    @PutMapping("/updateUser")
    public ResponseResult updateUser(@RequestBody @Validated AddUserVO addUserVO) {
        userService.updateUser(addUserVO);
        return ResponseResult.ok();
    }
}

